#!/usr/bin/env lua
-- -*- lua -*-
require("rfsm")
require("rfsmpp")
require("rfsm2json")
require("utils")

if arg and #arg < 1 then
   print("usage: rfsm2json <file1> <file2>...")
   os.exit(1)
end

function fsm2json(file)
   outfile = string.gsub(file, "\.%w*$", "")
   fsm_tpl = assert(rfsm.load(file), "failed to open fsm file " .. file)
   fsm = rfsm.init(fsm_tpl)
   if not fsm then
      print("error: state machine checking failed.")
      os.exit(1)
   end
   io.output(outfile .. ".json")
   io.write(rfsm2json.encode(fsm))
end

arg[-1]=nil; arg[0]=nil;
utils.map(fsm2json, arg)

--io.write(json.encode(fsm_tpl))
os.exit(0)